home *** CD-ROM | disk | FTP | other *** search
- /* xlstr - xlisp string builtin functions */
- /* Copyright (c) 1985, by David Michael Betz
- All Rights Reserved
- Permission is granted for unrestricted non-commercial use */
-
- #include "xlisp.h"
-
- /* external variables */
- extern NODE *xlstack;
-
- /* external procedures */
- extern char *strcat();
-
- /* xstrcat - concatenate a bunch of strings */
- NODE *xstrcat(args)
- NODE *args;
- {
- NODE *oldstk,val,*p;
- char *str;
- int len;
-
- /* create a new stack frame */
- oldstk = xlsave(&val,NULL);
-
- /* find the length of the new string */
- for (p = args, len = 0; p; )
- len += strlen(xlmatch(STR,&p)->n_str);
-
- /* create the result string */
- val.n_ptr = newnode(STR);
- val.n_ptr->n_str = str = stralloc(len);
- *str = 0;
-
- /* combine the strings */
- while (args)
- strcat(str,xlmatch(STR,&args)->n_str);
-
- /* restore the previous stack frame */
- xlstack = oldstk;
-
- /* return the new string */
- return (val.n_ptr);
- }
-
- /* xsubstr - return a substring */
- NODE *xsubstr(args)
- NODE *args;
- {
- NODE *oldstk,arg,src,val;
- int start,forlen,srclen;
- char *srcptr,*dstptr;
-
- /* create a new stack frame */
- oldstk = xlsave(&arg,&src,&val,NULL);
-
- /* initialize */
- arg.n_ptr = args;
-
- /* get string and its length */
- src.n_ptr = xlmatch(STR,&arg.n_ptr);
- srcptr = src.n_ptr->n_str;
- srclen = strlen(srcptr);
-
- /* get starting pos -- must be present */
- start = xlmatch(INT,&arg.n_ptr)->n_int;
-
- /* get length -- if not present use remainder of string */
- forlen = (arg.n_ptr ? xlmatch(INT,&arg.n_ptr)->n_int : srclen);
-
- /* make sure there aren't any more arguments */
- xllastarg(arg.n_ptr);
-
- /* don't take more than exists */
- if (start + forlen > srclen)
- forlen = srclen - start + 1;
-
- /* if start beyond string -- return null string */
- if (start > srclen) {
- start = 1;
- forlen = 0; }
-
- /* create return node */
- val.n_ptr = newnode(STR);
- val.n_ptr->n_str = dstptr = stralloc(forlen);
-
- /* move string */
- for (srcptr += start-1; forlen--; *dstptr++ = *srcptr++)
- ;
- *dstptr = 0;
-
- /* restore the previous stack frame */
- xlstack = oldstk;
-
- /* return the substring */
- return (val.n_ptr);
- }
-
- /* xstring - return a string consisting of a single character */
- NODE *xstring(args)
- NODE *args;
- {
- NODE *oldstk,val;
- char *p;
- int ch;
-
- /* get the character (integer) */
- ch = xlmatch(INT,&args)->n_int;
- xllastarg(args);
-
- /* make a one character string */
- oldstk = xlsave(&val,NULL);
- val.n_ptr = newnode(STR);
- val.n_ptr->n_str = p = stralloc(1);
- *p++ = ch; *p = '\0';
- xlstack = oldstk;
-
- /* return the new string */
- return (val.n_ptr);
- }
-
- /* xchar - extract a character from a string */
- NODE *xchar(args)
- NODE *args;
- {
- char *str;
- int n;
-
- /* get the string and the index */
- str = xlmatch(STR,&args)->n_str;
- n = xlmatch(INT,&args)->n_int;
- xllastarg(args);
-
- /* range check the index */
- if (n < 0 || n >= strlen(str))
- xlerror("index out of range",cvfixnum((FIXNUM)n));
-
- /* return the character */
- return (cvfixnum((FIXNUM)str[n]));
- }